
module.exports = {
  webServiceRequest,
  superAjax
}


function webServiceRequest(args){
  args.isWebService=true
  superAjax(args);
}

function superAjax(args) {
  if(args.url.substr(0,4)!='http'){
    args.url = 'https://test11.m.mypatroller.com/'+args.url
  }
  var def = {
    showSuccessAlert: true,
    successTitle: args.operateType + "成功！",
    errorTitle: args.operateType + "失败！",

    showWarnAlert: args.showWarnAlert ? args.showWarnAlert:false,
    warnTitle: "确定" + args.operateType + "？",
    warnAlertCancelCall: args.warnAlertCancelCall ? args.warnAlertCancelCall : function() {},
    url: args.url,
    data: args.data ? args.data : {},
    header: args.header ? args.header : {},
    method: args.method ? args.method : 'POST',
    dataType: args.dataType ? args.dataType : 'json',
    responseType: args.responseType ? args.responseType : 'text',
    success: function(res) {
      console.log("success", res);
      if (res.statusCode == 200) {
         var  data=res.data;
        if(args.isWebService){
          data= JSON.parse(res.data.substring(res.data.indexOf('string') + 39, (res.data.lastIndexOf('string') - 2)));
        }
        if (args.success){
          args.success(data);
        }
    
      }else{
        wx.showToast({
          icon: 'none',
          title: JSON.stringify(res)
        })
      }
    },
    fail: function(res) {
      console.log("fail", res);
      if (args.fail) {
        args.fail(res.data)
      }else{
        wx.showToast({
          icon: 'none',
          title: JSON.stringify(res)
        })
      }
    },
    complete: function(res) {
      if (args.complete) {
        args.complete(res.data)
      }
    }
  }


  if (args.isWebService){
    def.header['Content-Type'] ='application/x-www-form-urlencoded';
    def.method = 'POST';
  }

  def.data.companyname= 'test11';
  console.log("url=",def);

  if (def.showWarnAlert) {
    wx.showModal({
      title: '',
      content: def.warnTitle,
      success(res) {
        if (res.confirm) {
          wx.request(def);
        } else if (res.cancel) {
          def.warnAlertCancelCall();
        }
      }
    })
  } else {
    wx.request(def);
  }
}
